//me.js
//获取应用实例
const app = getApp()

Page({
  data: {
    headUrl: '../../../images/touxiang.jpg',
    nickName:'',
    checkedMale:true,
    checkedFeMale:false,
    birthday:'',
    phone:'',
    unit:'',
    gender:''
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      var userInfo = app.globalData.userInfo;
      var checkedMale=true;
      var checkedFeMale=false;
      var gender="MALE";
      var headUrl ='../../../images/touxiang.jpg';
      if(userInfo.gender=='FEMALE')
      {
        checkedMale=false;
        checkedFeMale=true;
        gender="FEMALE";
      }
      if(userInfo.avatar)
      {
        headUrl=userInfo.avatar;
      }

      this.setData({
        headUrl: headUrl,
        nickName:userInfo.name,
        phone:userInfo.phone,
        gender:gender,
        checkedMale:checkedMale,
        checkedFeMale:checkedFeMale,
        birthday:userInfo.birthday
      })
    }
  },
  //日期选择
  bindDateChange:function(e)
  {
    this.setData({
      birthday: e.detail.value
    })
  },
  //点击修改用户资料
  editBox:function(e)
  {
    const that =this;
    var name=e.detail.value.name;
    var avatar=e.detail.value.avatar;
    var gender=that.data.gender;
    var birthday=e.detail.value.birthday;
    var phone=e.detail.value.phone;
    var unit=e.detail.value.unit;
    wx.request({
      url: 'http://checking.fothing.com/api/v1/user',
      method:'post',
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer CDqGSxkL1weIO3eb4KjmLqnxBhWd1Da1"
      },
      data:{
        name:name,
        avatar:avatar,
        gender:gender,
        birthday:birthday,
        phone:phone,
        unit:unit
      },
      success: res => {
        if(res.data.code==0)
        {
          this.searchNew();
        //提示成功
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
          wx.redirectTo({
            url: '../../../pages/index/index',
          })
        }
        else
        {
          wx.showToast({
            title: '修改失败',
            icon: 'none'
          })
        }
       
        console.log(res);
      },
      fail: res => {
        wx.showToast({
          title: '修改用户信息失败',
          icon: 'none'
        })
      }
    })
  },
  //点击单选按钮
  radioChange:function(e){
    console.log(e.detail.value);
    this.setData({
      gender: e.detail.value
    })
  },
  //点击更换头像
  chooseImage(){
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        // 无论用户是从相册选择还是直接用相机拍摄，路径都是在这里面
        var filePath = res.tempFilePaths[0];
        console.log(filePath);
        //将刚才选的照片/拍的 放到下面view视图中
        that.setData({
          headUrl: filePath, //把照片路径存到变量中，
          carWin_img_hidden: false //让展示照片的view显示
        });
        // 这个是使用微信接口保存文件到数据库
        wx.uploadFile({
          url: "http://checking.fothing.com/api/v1/user/avator",
          filePath: filePath,
          header: {
            'Content-Type': 'multipart/form-data',
            "Authorization": "Bearer CDqGSxkL1weIO3eb4KjmLqnxBhWd1Da1"},
          name: 'uploadFile',
          success: function (res) {
            this.setData({
              headUrl:res.data.avatar
            });
          }
        })
      },
      fail: function (error) {
        console.error("调用本地相册文件时出错")
        console.warn(error)
      },
      complete: function () {
      }
    });
  },
  //更新全局记录
  searchNew:function() {
    wx.request({
      url: 'http://checking.fothing.com/api/v1/user/@self',
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer CDqGSxkL1weIO3eb4KjmLqnxBhWd1Da1"
      },
      success: res => {
        //2.存用户信息到全局变量
        app.globalData.userInfo = res.data.data;
        console.log(app.globalData.userInfo);
        //隐藏loading框
        wx.hideLoading();
      },
      fail: res => {
      }
    })
  }
})
